package lab03.test07;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Test07 {
    public static void main(String[] args) throws IOException {
        String path = "F:\\book.zip";  //表示压缩包的路径

        File myFile = null;  //表示输出的文件对象
        ZipFile zipFile = new ZipFile(path);

        InputStream inputStream = null;//定义输入流
        FileOutputStream outputStream = null; //定义输出流
        ZipInputStream zipInputStream = null;

        try {
            zipInputStream = new ZipInputStream(new FileInputStream(path));
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                System.out.println("解压文件" + entry.getName());  //得到压缩包中文件的名字

                myFile = new File("F:\\book" + File.separator + entry.getName());  //file.separator文件分隔符，可以针对不同的系统的不同的分隔符
                myFile.createNewFile();        //在book文件下建立压缩包的每一个文件

                inputStream = zipFile.getInputStream(entry);  //利用zipfile读取zip中的每一个entry的内容
                outputStream = new FileOutputStream(myFile);  //实例化输出流

                int ch = 0;
                while ((ch = inputStream.read()) != -1) {
                    outputStream.write(ch);        //向out的路径中的文件写入
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipInputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
